/**
 * MyInterceptor.java
 * com.ebook.interceptors
 * Copyright (c) 2013, 公司版权所有.
*/

package com.ebook.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ebook.core.domain.User;



/**
 * springmvc拦截器类
 * @author    潘吉友
 * @Date	 2013-3-13 	 
 */
public class MyInterceptor implements HandlerInterceptor{

	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
		// TODO Auto-generated method stub
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		
		// TODO Auto-generated method stub
		
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception{
		System.out.println(request.getServletPath());
		if (request.getServletPath().startsWith("/user/login")) {
			return true;
		}
		if (request.getServletPath().startsWith("/user/regist")) {
			return true;
		}
		if (request.getSession().getAttribute("loginuser") == null) {
			response.sendRedirect(request.getContextPath() + "/user/login");
			return false;
		}else{
			User user = (User) request.getSession().getAttribute("loginuser");
			int uid = Integer.valueOf(request.getParameter("uid")==null ?"0":request.getParameter("uid"));
			if(uid != user.getId()){
				response.sendRedirect(request.getContextPath() + "/user/login");
				return false;
			}
			return true;
			
		}
		
	}

	


	

}

